package com.chance.cc.crawler.development.bootstrap.dxy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import com.chance.cc.crawler.core.selector.Html;
import com.chance.cc.crawler.core.selector.Selectable;
import com.chance.cc.crawler.development.bootstrap.xcar.XCarHttpTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lt
 * @version 1.0
 * @date 2021-02-04 18:54:31
 * @email okprog@sina.com
 */
public class DXYHttpTest {
    public static Logger logger = LoggerFactory.getLogger(XCarHttpTest.class);

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HEW657EL99F83S9D");
        proxy.setPassword("8916B1F3F10B1979");
    }

    public static void main(String[] args) {

        HttpConfig httpConfig = HttpConfig.me("test");
        httpConfig.setProxy(proxy);
        httpConfig.setUseProxy(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        for (int i=1;i<=20;i++){
            new Thread(new TestForumRun(downloader,httpConfig),"thread_"+i).start();
        }
    }

    static class TestSearchNews implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestSearchNews(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://search.dxy.cn/?words=镁信&page=%s&source=CMS&limit=15&o=1";
            HttpRequest httpRequest = new HttpRequest();

            for(int i=1;i<10;i++){
                httpRequest.setUrl(String.format(url,i));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
//                System.out.println(httpPage.getRawText());
                Html responseHtml = httpPage.getHtml();
                List<Selectable> nodes = responseHtml.xpath("//div[@class=\"main-list\"]/div").nodes();
                for (Selectable node : nodes) {

                    String articleUrl = node.xpath("./h3/a/@href").get();
                    String releaseTime = node.xpath("./p[@class=\"it-author\"]/text()").all().toString();


                    System.out.println(articleUrl);
                    System.out.println(releaseTime);
                }

            }
        }
    }

    static class TestArticleRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestArticleRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://y.dxy.cn/channelarticledetail/765145?keywords=%E9%95%81%E4%BF%A1";
            HttpRequest httpRequest = new HttpRequest();

            for(int i=0;i<1;i++){
                httpRequest.setUrl(url);
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                Html html = httpPage.getHtml();
                String title = html.xpath("//h1[@class=\"article-title\"]/text()").get();
                String author = html.xpath("//h3[@class=\"name\"]/a/text()").get();
                System.out.println(title + "---" + author);
            }
        }
    }

    static class TestSearchBBS implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestSearchBBS(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://search.dxy.cn/?words=京东健康&page=%s&source=BBS&limit=15&o=1";
            HttpRequest httpRequest = new HttpRequest();
//            httpRequest.addHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36");
//            httpRequest.addHeader("cookie","nguv=c_160999737235228227110117218341611798; _Xdwuv=5ff69c3ce5e31; _Xdwnewuv=1; _PVXuv=5ff69c3df8614; __jsluid_s=49e976754d92ae5b3a1f9be9e2f407db; __jsluid_h=ff270f9a2198fb6530f6e9e06bae425a; __clickidc=161000895164367053; fw_slc=1%3A1610601454%3B1%3A1610601458%3B1%3A1610601459%3B1%3A1610601461%3B1%3A1610601462; fw_pvc=1%3A1610601300%3B1%3A1610601458%3B1%3A1610601461%3B1%3A1610601667%3B1%3A1610601941; fw_clc=1%3A1610601456%3B1%3A1610601460%3B1%3A1610601666%3B1%3A1610601940%3B1%3A1610601992; fw_exc=1%3A1610601456%3B1%3A1610601457%3B1%3A1610601671%3B1%3A1610603232%3B1%3A1610603244; Hm_lvt_53eb54d089f7b5dd4ae2927686b183e0=1610247261,1610333953,1610601302,1610676357; place_prid_lin=2; place_crid_lin=507; place_prname=%E4%B8%8A%E6%B5%B7%E5%B8%82; iwt_uuid=55c322d6-dcca-4fb4-8174-19f8fadc38eb; _locationInfo_=%7Burl%3A%22http%3A%2F%2Fsh.xcar.com.cn%2F%22%2Ccity_id%3A%22507%22%2Cprovince_id%3A%222%22%2C%20city_name%3A%22%25E4%25B8%258A%25E6%25B5%25B7%22%7D; isRemoveCookie=1; place_crname=%E4%B8%8A%E6%B5%B7%E8%BD%A6%E5%B8%82; uv_firstv_refers=https%3A//xtv.xcar.com.cn/show/202101/id_228119.html; __jsl_clearance_s=1610696223.082|0|olPDY0Wa3k3aAbSDhBWQ8W%2BY8UE%3D; zg_did=%7B%22did%22%3A%20%22176db52511822c-0749a9c4fbd349-c791039-100200-176db525119196%22%7D; zg_8f3d0255011c4bc5bae66beca6584825=%7B%22sid%22%3A%201610696223475%2C%22updated%22%3A%201610696260422%2C%22info%22%3A%201610603234040%2C%22superProperty%22%3A%20%22%7B%5C%22platform_type%5C%22%3A%20%5C%22PC%5C%22%2C%5C%22login_id%5C%22%3A%20null%2C%5C%22project_name%5C%22%3A%20%5C%22XCAR%5C%22%2C%5C%22login_status%5C%22%3A%200%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.xcar.com.cn%22%2C%22landHref%22%3A%20%22https%3A%2F%2Fxtv.xcar.com.cn%2F%22%2C%22zs%22%3A%200%2C%22sc%22%3A%200%2C%22firstScreen%22%3A%201610696223475%7D; _Xdwstime=1610696261; Hm_lpvt_53eb54d089f7b5dd4ae2927686b183e0=1610696262");

            for(int i=1;i<10;i++){
                List<Map<String,String>> crawlerDataList = new ArrayList<>();

                httpRequest.setUrl(String.format(url,i));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                Html responseHtml = httpPage.getHtml();
                List<Selectable> nodes = responseHtml.xpath("//div[@class=\"main-list\"]/div").nodes();
                for (Selectable node : nodes) {

                    String articleUrl = node.xpath("./h3/a/@href").get();
                    String releaseTime = node.xpath("./p[@class=\"it-author\"]/text()").all().toString();


                    System.out.println(articleUrl);
                    System.out.println(releaseTime);
                }
            }

        }

    }

    static class TestForumRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestForumRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "http://3g.dxy.cn/bbs/bbsapi/mobile?s=view_topic&checkUserAction=0&withGood=1&order=1&size=3&id=43718047&page=%s";
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.addHeader("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1");
            for(int i=1;i<20;i++){
                List<Map<String,String>> crawlerDataList = new ArrayList<>();

                httpRequest.setUrl(String.format(url,i));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                JSONObject jsonObject = JSONObject.parseObject(httpPage.getRawText());
                JSONArray items = jsonObject.getJSONArray("items");
                System.out.println("本页大小： " + items.size());
            }
        }
    }

}
